<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <template v-if="record.adjustStatus=='0'">
              <a-button type="primary" size="small" @click="handleAdjust(record)"
                        :disabled="isDisabledAuth('erpEmployeeFile:adjust')">调整</a-button>
          </template>
          <template v-if="record.adjustStatus=='1'">
              <a-button type="primary" size="small" @click="handleAdjustRecord(record)"
                        :disabled="isDisabledAuth('erpEmployeeFile:adjustRecord')">调整记录</a-button>
          </template>
        <!-- <a-divider type="vertical"/>
        <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="delete"
                    :disabled="isDisabledAuth('erpEmployeeAttendanceDay:delete')"/>
        </a-popconfirm> -->
        </span>
      </a-table>
    </div>
    <!-- 调整弹出框 -->
    <a-modal v-model="adjustFlag" :title="adjustTitle" :width="850" @ok="handleAdjustOk" :bodyStyle ="{
        'display':'flex',
        'justifyContent': 'center',
        'alignItems': 'center',
      }">
      <j-form-container >
        <a-form-model ref="form" :model="adjustModel" :rules="validatorRules" slot="detail">
          <a-row :gutter="48">
            <a-col :span="8">
              <a-form-model-item label="调整时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustTime">
                <j-date placeholder="请输入调整时间" v-model="adjustModel.adjustTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="调整人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustEmployee">
                <select-user-by-dep :multi="false"
                  v-model="adjustModel.adjustEmployee" disabled></select-user-by-dep>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="校对状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standardStatus">
                <j-dict-select-tag :disabled="formDisabled" type="list" v-model="adjustModel.standardStatus" dictCode="standard_status" placeholder="请选择校对状态" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="调整说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adjustRemark">
                <a-textarea :disabled="formDisabled" :max-length="200" v-model="adjustModel.adjustRemark" rows="4"  :autosize="{ minRows: 4, maxRows: 4 }"  placeholder="请输入调整说明"/>
              </a-form-model-item>
            </a-col>
            <a-input v-model="adjustModel.id"  type="hidden"></a-input>
            <a-input v-model="adjustModel.monthId" type="hidden"></a-input>
          </a-row>
        </a-form-model>
      </j-form-container>
      <template slot="footer" v-if="formDisabled">
        <a-button @click="handleCancel">关闭</a-button>
      </template>
    </a-modal>
    <erp-employee-attendance-day-modal ref="modalForm" @ok="modalFormOk"></erp-employee-attendance-day-modal>
  </a-card>
</template>

<script>

  import { httpAction,postAction, getAction } from '@/api/manage'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ErpEmployeeAttendanceDayModal from './modules/ErpEmployeeAttendanceDayModal'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import moment from 'moment'
  import { mapGetters } from 'vuex'

  export default {
    name: 'ErpEmployeeAttendanceDayList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpEmployeeAttendanceDayModal,
      SelectUserByDep
    },
    data () {
      var validatorStatus = async (rule,value,callback)=>{
        if(value){
          const that = this;
          if(this.model.status=='1' && value=='0') {
            callback('当前状态为正常,请选择其他校准状态！')
            return;
          }else if(value=='1' && this.model.isLate=='1'){
            callback('当前状态为迟到,请选择其他校准状态！')
            return;
          }else if(value=='2' && this.model.isLeave=='1'){
            callback('当前状态为早退,请选择其他校准状态！')
            return;
          }else if(value=='4' && this.model.isAbsentWork=='1'){
            callback('当前状态为旷工,请选择其他校准状态！')
            return;
          }else if(value=='3' && this.model.isMissing=='1'){
            callback('当前状态为缺卡,请选择其他校准状态！')
            return;
          }else if(value=='5' && this.model.isLocationAnomaly=='1'){
            callback('当前状态为地点异常,请选择其他校准状态！')
            return;
          }else if(value=='6' && this.model.isUnitException=='1'){
            callback('当前状态为设备异常,请选择其他校准状态！')
            return;
          }
          callback()
        }
        callback()
      };
      return {
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        model:{},
        adjustTitle:"调整",
        validatorRules: {
          standardStatus:[{ required: true, message: '请选择校准状态!'},{ validator: validatorStatus, trigger: "blur" },],
          adjustRemark:[{ required: true, message: '请输入调整说明!'}],
        },
        formDisabled:false,
        adjustFlag:false,
        adjustModel:{},
        createTimeRange:[],
        description: '员工考勤日报表管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
        {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'时间',
            align:"center",
            dataIndex: 'dayDate',
          },
          {
            title:'星期',
            align:"center",
            dataIndex: 'dayOfWeek_dictText',
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'employeeId_dictText',
          },
          {
            title:'企业微信账号',
            align:"center",
            dataIndex: 'wxUserId',
          },
          {
            title:'部门',
            align:"center",
            dataIndex: 'departId_dictText',
          },
          {
            title:'职务',
            align:"center",
            dataIndex: 'postId_dictText',
          },
          {
            title:'规则名称',
            align:"center",
            dataIndex: 'groupName',
          },
          {
            title:'班次',
            align:"center",
            dataIndex: 'orderOfClasses',
          },
          {
            title:'日报类型',
            align:"center",
            dataIndex: 'dayType_dictText',
          },
          {
            title:'当日打卡次数',
            align:"center",
            dataIndex: 'checkinCount',
          },
          {
            title:'工作时长(小时)',
            align:"center",
            dataIndex: 'regularWorkHour',
          },
          {
            title:'标准工作时长(小时)',
            align:"center",
            dataIndex: 'standardWorkHour',
          },
          {
            title:'最早打卡时间',
            align:"center",
            dataIndex: 'earliestTime',
          },
          {
            title:'最晚打卡时间',
            align:"center",
            dataIndex: 'lastestTime',
          },
          {
            title:'假勤信息',
            align:"center",
            dataIndex: 'spDescription',
          },
          {
            title:'异常信息',
            align:"center",
            dataIndex: 'exceptionDescription',
          },
          {
            title:'迟到次数',
            align:"center",
            dataIndex: 'lateCount',
          },
          {
            title:'迟到时长(分钟)',
            align:"center",
            dataIndex: 'lateDuration',
          },
          {
            title:'早退次数',
            align:"center",
            dataIndex: 'leaveEarlyCount',
          },
          {
            title:'早退时长(分钟)',
            align:"center",
            dataIndex: 'leaveEarlyDuration',
          },
          {
            title:'缺卡次数',
            align:"center",
            dataIndex: 'missingCardCount',
          },
          {
            title:'旷工次数',
            align:"center",
            dataIndex: 'absentWorkCount',
          },
          {
            title:'旷工时长(分钟)',
            align:"center",
            dataIndex: 'absentWorkDuration',
          },
          {
            title:'地点异常次数',
            align:"center",
            dataIndex: 'locationAnomalyCount',
          },
          {
            title:'设备异常次数',
            align:"center",
            dataIndex: 'unitExceptionCount',
          },
          {
            title:'加班状态',
            align:"center",
            dataIndex: 'otStatus_dictText',
          },
          {
            title:'加班时长(分钟)',
            align:"center",
            dataIndex: 'otDuration',
          },
          {
            title:'补卡次数',
            align:"center",
            dataIndex: 'replenishmentCardCount',
          },
          {
            title:'外勤次数',
            align:"center",
            dataIndex: 'fieldWorkCount',
          },
          {
            title:'外出小时',
            align:"center",
            dataIndex: 'outDuration',
          },
          {
            title:'出差天数',
            align:"center",
            dataIndex: 'travelDuration',
          },
          {
            title:'年假天数',
            align:"center",
            dataIndex: 'annualLeaveDuration',
          },
          {
            title:'事假(小时)',
            align:"center",
            dataIndex: 'askLeaveDuration',
          },
          {
            title:'病假(小时)',
            align:"center",
            dataIndex: 'sickLeaveDuration',
          },
          {
            title:'调休假(小时)',
            align:"center",
            dataIndex: 'leaveDuration',
          },
          {
            title:'婚假天数',
            align:"center",
            dataIndex: 'marriageLeaveDuration',
          },
          {
            title:'产假天数',
            align:"center",
            dataIndex: 'maternityLeaveDuration',
          },
          {
            title:'陪产假天数',
            align:"center",
            dataIndex: 'accompanyingLaborDuration',
          },
          {
            title:'哺乳假天数',
            align:"center",
            dataIndex: 'breastfeedingLeaveDuration',
          },
          {
            title:'其他天数',
            align:"center",
            dataIndex: 'otherDuration',
          },
          // {
          //   title: '操作',
          //   dataIndex: 'action',
          //   align:"center",
          //   fixed:"right",
          //   width:147,
          //   scopedSlots: {
          //   filterDropdown: 'filterDropdown',
          //   filterIcon: 'filterIcon',
          //   customRender: 'action' }
          // }
        ],
        url: {
          adjustAttendance:"/api/erp/hr/attendance/erpEmployeeAttendanceDay/adjustAttendance",
          list: "/api/erp/hr/attendance/erpEmployeeAttendanceDay/list",
          delete: "/api/erp/hr/attendance/erpEmployeeAttendanceDay/delete",
          deleteBatch: "/api/erp/hr/attendance/erpEmployeeAttendanceDay/deleteBatch",
          exportXlsUrl: "/api/erp/hr/attendance/erpEmployeeAttendanceDay/exportXls",
          importExcelUrl: "api/erp/hr/attendance/erpEmployeeAttendanceDay/importExcel",
        },
        dictOptions:{},
        superFieldList:[],
        disableMixinCreated:true
      }
    },
    watch: {
      $route(to, from) {
        this.queryParam=to.query;
        this.loadData();
      },
    },
    created(){
      let query=this.$route.query
      this.queryParam=query;
      this.loadData();
      // 初始化字典配置 在自己页面定义
      this.initDictConfig()
      this.initColumns()
    },
    methods: {
      ...mapGetters(['userInfo']),
      handleCancel(){
        this.adjustFlag=false;
      },
      //调整
      handleAdjust(record){
        if(this.$refs.form!=undefined){
          this.$refs.form.clearValidate('standardStatus');
          this.$refs.form.clearValidate('adjustRemark');
        }
        this.adjustTitle="调整";
        this.adjustFlag=true;
        this.formDisabled=false;
        this.model=record;
        this.adjustModel=Object.assign(record, {});
        this.$set(this.adjustModel, 'id', record.id);
        this.$set(this.adjustModel, 'monthId', record.monthId);
        this.$set(this.adjustModel, 'adjustTime', moment().format('YYYY-MM-DD HH:mm:ss'));
        this.$set(this.adjustModel, 'adjustEmployee', this.userInfo().empId);
        this.$set(this.adjustModel, 'standardStatus', null);
        this.$set(this.adjustModel, 'adjustRemark', '');
      },
      handleAdjustRecord(record){
        this.adjustTitle="调整详情";
        if(this.$refs.form!=undefined){
          this.$refs.form.clearValidate('standardStatus');
          this.$refs.form.clearValidate('adjustRemark');
        }
        this.adjustFlag=true;
        this.formDisabled=true;
        this.$set(this.adjustModel, 'id', record.id);
        this.$set(this.adjustModel, 'adjustTime', record.adjustTime);
        this.$set(this.adjustModel, 'adjustEmployee', record.adjustEmployee);
        this.$set(this.adjustModel, 'standardStatus', record.standardStatus);
        this.$set(this.adjustModel, 'adjustRemark', record.adjustRemark);
      },
      //调整完成
      handleAdjustOk(){
        this.$refs.form.validate(valid => {
          if (valid) {
            let adjustModel=this.adjustModel;
            adjustModel.adjustStatus='1';
            postAction(this.url.adjustAttendance,adjustModel).then(res => {
              if(res.success){
                this.$message.success(res.message);
                // let query=this.$route.query
                // this.queryParam=query;
                this.loadData();
              }else{
                this.$message.warning(res.message);
              }
            })
            this.adjustFlag = false;
          }
        })
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'monthId',text:'月表数据',dictCode:''})
        fieldList.push({type:'string',value:'dayDate',text:'时间',dictCode:''})
        fieldList.push({type:'string',value:'employeeId',text:'姓名',dictCode:''})
        fieldList.push({type:'string',value:'wxUserId',text:'企业微信账号',dictCode:''})
        fieldList.push({type:'string',value:'departId',text:'部门',dictCode:''})
        fieldList.push({type:'string',value:'postId',text:'职务',dictCode:''})
        fieldList.push({type:'string',value:'groupId',text:'规则id',dictCode:''})
        fieldList.push({type:'string',value:'groupName',text:'规则名称',dictCode:''})
        fieldList.push({type:'string',value:'orderOfClasses',text:'班次',dictCode:''})
        fieldList.push({type:'string',value:'dayType',text:'日报类型',dictCode:''})
        fieldList.push({type:'int',value:'checkinCount',text:'当日打卡次数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'regularWorkHour',text:'工作时长',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'standardWorkHour',text:'标准工作时长',dictCode:''})
        fieldList.push({type:'string',value:'earliestTime',text:'最早打卡时间',dictCode:''})
        fieldList.push({type:'string',value:'lastestTime',text:'最晚打卡时间',dictCode:''})
        fieldList.push({type:'Text',value:'spDescription',text:'假勤信息',dictCode:''})
        fieldList.push({type:'Text',value:'exceptionDescription',text:'异常信息',dictCode:''})
        fieldList.push({type:'int',value:'lateCount',text:'迟到次数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'lateDuration',text:'迟到时长',dictCode:''})
        fieldList.push({type:'int',value:'leaveEarlyCount',text:'早退次数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'leaveEarlyDuration',text:'早退时长',dictCode:''})
        fieldList.push({type:'int',value:'missingCardCount',text:'缺卡次数',dictCode:''})
        fieldList.push({type:'int',value:'absentWorkCount',text:'旷工次数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'absentWorkDuration',text:'旷工时长',dictCode:''})
        fieldList.push({type:'int',value:'locationAnomalyCount',text:'地点异常次数',dictCode:''})
        fieldList.push({type:'int',value:'unitExceptionCount',text:'设备异常次数',dictCode:''})
        fieldList.push({type:'string',value:'otStatus',text:'加班状态',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'otDuration',text:'加班时长',dictCode:''})
        fieldList.push({type:'int',value:'replenishmentCardCount',text:'补卡次数',dictCode:''})
        fieldList.push({type:'int',value:'fieldWorkCount',text:'外勤次数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'outDuration',text:'外出小时',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'travelDuration',text:'出差天数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'annualLeaveDuration',text:'年假天数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'askLeaveDuration',text:'事假(小时)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'sickLeaveDuration',text:'病假(小时)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'leaveDuration',text:'调休假(小时)',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'marriageLeaveDuration',text:'婚假天数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'maternityLeaveDuration',text:'产假天数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'accompanyingLaborDuration',text:'陪产假天数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'breastfeedingLeaveDuration',text:'哺乳假天数',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'otherDuration',text:'其他天数',dictCode:''})
        fieldList.push({type:'string',value:'status',text:'状态',dictCode:''})
        fieldList.push({type:'string',value:'adjustStatus',text:'调整状态',dictCode:''})
        fieldList.push({type:'date',value:'adjustTime',text:'调整时间'})
        fieldList.push({type:'string',value:'adjustEmployee',text:'调整人',dictCode:''})
        fieldList.push({type:'Text',value:'adjustRemark',text:'调整说明',dictCode:''})
        fieldList.push({type:'string',value:'standardStatus',text:'校准状态',dictCode:''})
        fieldList.push({type:'string',value:'isLate',text:'是否迟到',dictCode:''})
        fieldList.push({type:'string',value:'isLeave',text:'是否早退',dictCode:''})
        fieldList.push({type:'string',value:'isAbsentWork',text:'是否旷工',dictCode:''})
        fieldList.push({type:'string',value:'isMissing',text:'是否缺卡',dictCode:''})
        fieldList.push({type:'string',value:'isLocationAnomaly',text:'是否地点异常',dictCode:''})
        fieldList.push({type:'string',value:'isUnitException',text:'是否设备异常',dictCode:''})
        fieldList.push({type:'string',value:'isWorkOver',text:'是否工作日加班',dictCode:''})
        fieldList.push({type:'string',value:'isHolidaysOver',text:'是否节假日加班',dictCode:''})
        fieldList.push({type:'string',value:'isRestdaysOver',text:'是否休息日加班',dictCode:''})
        fieldList.push({type:'string',value:'isReplenishmentCard',text:'是否补卡',dictCode:''})
        fieldList.push({type:'string',value:'isFieldWork',text:'是否外勤打卡',dictCode:''})
        fieldList.push({type:'string',value:'isOut',text:'是否外出',dictCode:''})
        fieldList.push({type:'string',value:'isTravel',text:'是否出差',dictCode:''})
        fieldList.push({type:'string',value:'isAnnualLeave',text:'是否年假',dictCode:''})
        fieldList.push({type:'string',value:'isAskLeave',text:'是否事假',dictCode:''})
        fieldList.push({type:'string',value:'isSickLeave',text:'是否病假',dictCode:''})
        fieldList.push({type:'string',value:'isRestLeave',text:'是否调休',dictCode:''})
        fieldList.push({type:'string',value:'isMarriageLeave',text:'是否婚假',dictCode:''})
        fieldList.push({type:'string',value:'isMaternityLeave',text:'是否产假',dictCode:''})
        fieldList.push({type:'string',value:'isAccompanyingLabor',text:'是否陪产假',dictCode:''})
        fieldList.push({type:'string',value:'isBreastfeedingLeave',text:'是否哺乳假',dictCode:''})
        fieldList.push({type:'string',value:'isOther',text:'是否其他假',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
